#include "outputsettings.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

QString OutputSettings::toJsonString() const
{
    QJsonObject json;
    
    // 页边距
    QJsonObject marginsObj;
    marginsObj["top"] = margins.top;
    marginsObj["bottom"] = margins.bottom;
    marginsObj["left"] = margins.left;
    marginsObj["right"] = margins.right;
    json["margins"] = marginsObj;
    
    // 页眉
    QJsonObject headerObj;
    headerObj["enabled"] = header.enabled;
    headerObj["text"] = header.text;
    headerObj["font"] = header.font.toString();
    headerObj["color"] = header.color.name();
    headerObj["position"] = static_cast<int>(header.position);
    headerObj["showOnCoverPage"] = header.showOnCoverPage;
    json["header"] = headerObj;
    
    // 页脚
    QJsonObject footerObj;
    footerObj["enabled"] = footer.enabled;
    footerObj["text"] = footer.text;
    footerObj["font"] = footer.font.toString();
    footerObj["color"] = footer.color.name();
    footerObj["position"] = static_cast<int>(footer.position);
    footerObj["showOnCoverPage"] = footer.showOnCoverPage;
    json["footer"] = footerObj;
    
    // 封面页
    QJsonObject coverObj;
    coverObj["enabled"] = coverPage.enabled;
    coverObj["title"] = coverPage.title;
    coverObj["subtitle"] = coverPage.subtitle;
    coverObj["author"] = coverPage.author;
    coverObj["date"] = coverPage.date;
    coverObj["titleFont"] = coverPage.titleFont.toString();
    coverObj["subtitleFont"] = coverPage.subtitleFont.toString();
    coverObj["authorFont"] = coverPage.authorFont.toString();
    coverObj["dateFont"] = coverPage.dateFont.toString();
    coverObj["templateName"] = coverPage.templateName;
    json["coverPage"] = coverObj;
    
    json["templateName"] = templateName;
    
    return QJsonDocument(json).toJson(QJsonDocument::Compact);
}

bool OutputSettings::fromJsonString(const QString& jsonStr)
{
    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
    
    if (error.error != QJsonParseError::NoError) {
        return false;
    }
    
    QJsonObject json = doc.object();
    
    // 页边距
    if (json.contains("margins")) {
        QJsonObject marginsObj = json["margins"].toObject();
        margins.top = marginsObj["top"].toDouble(20.0);
        margins.bottom = marginsObj["bottom"].toDouble(20.0);
        margins.left = marginsObj["left"].toDouble(20.0);
        margins.right = marginsObj["right"].toDouble(20.0);
    }
    
    // 页眉
    if (json.contains("header")) {
        QJsonObject headerObj = json["header"].toObject();
        header.enabled = headerObj["enabled"].toBool(false);
        header.text = headerObj["text"].toString();
        header.font.fromString(headerObj["font"].toString("Arial,12,-1,5,50,0,0,0,0,0"));
        header.color = QColor(headerObj["color"].toString("#000000"));
        header.position = static_cast<HeaderFooterPosition>(headerObj["position"].toInt(1));
        header.showOnCoverPage = headerObj["showOnCoverPage"].toBool(false);
    }
    
    // 页脚
    if (json.contains("footer")) {
        QJsonObject footerObj = json["footer"].toObject();
        footer.enabled = footerObj["enabled"].toBool(false);
        footer.text = footerObj["text"].toString();
        footer.font.fromString(footerObj["font"].toString("Arial,12,-1,5,50,0,0,0,0,0"));
        footer.color = QColor(footerObj["color"].toString("#000000"));
        footer.position = static_cast<HeaderFooterPosition>(footerObj["position"].toInt(1));
        footer.showOnCoverPage = footerObj["showOnCoverPage"].toBool(false);
    }
    
    // 封面页
    if (json.contains("coverPage")) {
        QJsonObject coverObj = json["coverPage"].toObject();
        coverPage.enabled = coverObj["enabled"].toBool(false);
        coverPage.title = coverObj["title"].toString();
        coverPage.subtitle = coverObj["subtitle"].toString();
        coverPage.author = coverObj["author"].toString();
        coverPage.date = coverObj["date"].toString();
        coverPage.titleFont.fromString(coverObj["titleFont"].toString("Arial,24,-1,5,75,0,0,0,0,0"));
        coverPage.subtitleFont.fromString(coverObj["subtitleFont"].toString("Arial,18,-1,5,50,0,0,0,0,0"));
        coverPage.authorFont.fromString(coverObj["authorFont"].toString("Arial,14,-1,5,50,0,0,0,0,0"));
        coverPage.dateFont.fromString(coverObj["dateFont"].toString("Arial,12,-1,5,50,0,0,0,0,0"));
        coverPage.templateName = coverObj["templateName"].toString("默认模板");
    }
    
    templateName = json["templateName"].toString("默认设置");
    
    return true;
}
